//项目路径
var project = 'http://localhost/website';

$(function(){
    //获取地址栏参数
    var cid = getParamter("cid");
    var csid= getParamter("csid");
    var pageNow = getParamter("pageNow");
    if(pageNow == ""){
        pageNow = 1;
    }
    if(cid!=""){//查询一级类目下新闻
        findNewsByCid(cid,pageNow);
    }
    if(csid!=""){//查询二级类目下新闻
        findNewsByCsid(csid,pageNow);
    }
    if(cid =="" && csid ==""){
        location.href =  project + '/index.html'
        return
    }
    //类目
    listCategory(cid,csid);

})


//根据一级类目查询新闻
function findNewsByCid(cid,pageNow) {
    $.ajax({
        url:project+'/news/findNewsByCid',
        dataType:'json',
        type:'get',
        data:{
            pageNow:pageNow,
            cid:cid
        },
        success:function(data){
            //************宣传语************//
            $("#publicityOne").text( data.categoryExt.publicityOne);//宣传语1
            $("#publicityTwo").text( data.categoryExt.publicityTwo);//宣传语2
            $(".img-responsive").attr("src","http://localhost:8080/website_manager"+data.categoryExt.image);
            //*******声明变量*********//
            var hasPreviousPage=data.hasPreviousPage;//是否是第一页
            var hasNextPage=data.hasNextPage;    //是否是最后一页
            var pageNum = data.pageNum;         //当前页数
            var newList = data.list;          //新闻列表
            var navigatepageNums =data.navigatepageNums;   //分页数组
            var pages = data.pages//总页数

            //新闻列表
            for(var i=0;i<newList.length;i++){
                $("#container").append(
                    `<div class="new-info ">
                    <div class="news-date">`+formateJsonDate(newList[i].createTime).split(" ")[0]+`</div>
                    <div class="news-r">
                    <a href="newsinfo.html?nid=`+newList[i].id+`">
                        <h4>`+newList[i].title+`</h4>
                        <p>`+newList[i].content.substring(0,100)+`…</p>
                    </a>
                        <a href="newsinfo.html?nid=`+newList[i].id+`">→</a>
                    </div>
                </div>`
                )
            }


            //********* 分页 ****************/
            //翻页按钮


            var str = `<span> 共`+pages+`页 页次:`+pageNum+`/`+pages+`页</span>\n`

            //上一页
            if (hasPreviousPage == true) {
                str = str + " <a href='/website/news.html?cid=" + cid + "&pageNow=1'>首页</a>\n" +
                    "                <a href='/website/news.html?cid=" + cid + "&pageNow=" + (pageNum - 1) + "'>上一页</a>\n"
            }
            //页码
            for (var i = 0; i < navigatepageNums.length; i++) {
                if (navigatepageNums[i] == pageNum) {
                    str = str + "<span><font color='red'>" + pageNum + "</font></span>"
                } else {
                    str = str + "<a href='/website/news.html?cid=" + cid + "&pageNow=" + navigatepageNums[i] + "'>" + navigatepageNums[i] + "</a>\n"
                }
            }
            //下一页
            if (hasNextPage == true) {
                str = str + "<a href='/website/news.html?cid=" + cid + "&pageNow=" + (pageNum + 1) + "'>下一页</a>\n" +
                    "<a href='/website/news.html?cid=" + cid + "&pageNow=" + pages + "'>尾页</a>"
            }
            //准备好的数据加入其中
            $(".last").append(str)

        }

    })
}

//根据二级类目查询新闻
function findNewsByCsid(csid,pageNow) {
    $.ajax({
        url:project+'/news/findNewsByCsid',
        dataType:'json',
        type:'get',
        data:{
            pageNow:pageNow,
            csid:csid
        },
        success:function(data){
            //**********宣传语***********
            $("#publicityOne").text( data.categorySecond.publicityOne);//宣传语1
            $("#publicityTwo").text( data.categorySecond.publicityTwo);//宣传语2
            $(".img-responsive").attr("src","http://localhost:8080/website_manager"+data.categorySecond.image);
            //*******声明变量*********//
            var hasPreviousPage=data.hasPreviousPage;//是否是第一页
            var hasNextPage=data.hasNextPage;    //是否是最后一页
            var pageNum = data.pageNum;         //当前页数
            var newList = data.list;          //新闻列表
            var navigatepageNums =data.navigatepageNums;   //分页数组
            var pages = data.pages//总页数
            //新闻列表
            for(var i=0;i<newList.length;i++){
                $("#container").append(
                    `<div class="new-info">
                    <div class="news-date">`+formateJsonDate(newList[i].createTime).split(" ")[0]+`</div>
                    <div class="news-r">
                     <a href="newsinfo.html?nid=`+newList[i].id+`&csid=`+csid+`">
                        <h4>`+newList[i].title+`</h4>
                        <span name="content">
                         <p >`+newList[i].content.replace(/<[^>]+>/g, "").substring(0,100)+`...</p>
                        </span>
                        <a href="newsinfo.html?nid=`+newList[i].id+`&csid=`+csid+`">→</a>
                    </div>
                </div>`
                )
            }
            /*$("#container").find("img").remove()
            $("[name='content']").each(function () {
                var content = $(this).text().length;
               /!* if($(this).text().length>100){
                    $(this).html($(this).text().substring(0,100)+"...");
                }*!/
                $(this).html($(this).text().substring(0,100)+"...")
                console.log(content)
            })*/
            //********* 分页 ****************/
            //翻页按钮


            var str = `<span> 共`+pages+`页 页次:`+pageNum+`/`+pages+`页</span>\n`

            //上一页
            if (hasPreviousPage == true) {
                str = str + " <a href='/website/news.html?csid=" + csid + "&pageNow=1'>首页</a>\n" +
                    "                <a href='/website/news.html?csid=" + csid + "&pageNow=" + (pageNum - 1) + "'>上一页</a>\n"
            }
            //页码
            for (var i = 0; i < navigatepageNums.length; i++) {
                if (navigatepageNums[i] == pageNum) {
                    str = str + "<span><font color='red'>" + pageNum + "</font></span>"
                } else {
                    str = str + "<a href='/website/news.html?csid=" + csid + "&pageNow=" + navigatepageNums[i] + "'>" + navigatepageNums[i] + "</a>\n"
                }
            }
            //下一页
            if (hasNextPage == true) {
                str = str + "<a href='/website/news.html?csid=" + csid + "&pageNow=" + (pageNum + 1) + "'>下一页</a>\n" +
                    "<a href='/website/news.html?csid=" + csid + "&pageNow=" + pages + "'>尾页</a>"
            }
            //准备好的数据加入其中
            $(".last").append(str)
        }

    })
}

